import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { PropertyKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { actionManager } from "@/mdesign-lib/mdesign-app.umd.js";
import { MenuItem } from "@/mdesign-lib/mdesign-app.umd.js";
import { modelService } from "@/mdesign-lib/mdesign-app.umd.js";
import { GraphModel } from "@/mdesign-lib/mdesign-graph.umd.js";
import { GraphTab } from "@/mdesign-lib/mdesign-app.umd.js";
import { propertyUtil } from "@/mdesign-lib/mdesign-app.umd.js";
import { UpdateModelPropertyDto } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";

export class ScenarioConversionAction extends Action {
  keyName = "scenarioConversion" as ActionKey;
  desc = "scenarioConversion";

  onToolbarClick(projectId: string, graph: GraphModel, tab: GraphTab) {
    this.run(projectId, graph, tab);
  }

  async run(projectId: string, graph: GraphModel, tab: GraphTab) {
    const openCreateDiagramaction = actionManager.getAction(ActionKey.openCreateDiagram);
    const diagramModel = tab.diagramModel;

    // 查询想定是否生成
    const existScenarioId = await archService.getExistScenario(projectId, diagramModel!.id);
    if (existScenarioId) {
      const result = await app.$confirm
        .popup({
          title: "想定更新提示",
          content: "想定已生成，是否进行更新？",
          subContent: "注意：数据更新将覆盖用户自定义编辑的想定数据。"
        })
        .catch(err => err);
      if (result.type === "ok") {
        const scenarioTableModel = await modelService.getModelData(projectId, existScenarioId);
        const isExistTab = app.activeProject?.ui.isExistTab(existScenarioId);
        actionManager.getAction(ActionKey.openDiagram)?.run(scenarioTableModel, false);
        if (isExistTab) {
          const activeTab = app.activeProject?.ui.getActiveTab();
          await actionManager.getAction(ActionKey.RefreshTable)?.run(activeTab?.diagramTableEditor);
        }
        app.$notify({ type: "success", message: "想定更新成功", title: "成功" });
      }
      return;
    }

    app.$loading.popup("想定生成中...");

    const contextId = (await modelService.getPropertyValueById(projectId, diagramModel!.id, PropertyKey.Diagram_context)) as string;
    const contextModel = await modelService.getModelData(projectId, contextId);
    const parentId = contextModel.ownerId;

    let list = await modelService.getCanCreateDiagramTypes(projectId, parentId);
    let scenarioConversionMenu: MenuItem | null = null;
    list.forEach(item => {
      item.children?.forEach(child => {
        if (child.appliedStereotypes?.[0] === "81640998590509376") {
          scenarioConversionMenu = child;
        }
      });
    });
    if (!scenarioConversionMenu) {
      app.$notify({ type: "error", message: "父级元素不支持创建该视图", title: "错误" });
      app.$loading.close();
      return;
    }
    const tableId = await openCreateDiagramaction.createGraph(scenarioConversionMenu, projectId, parentId);
    if (tableId && typeof tableId === "string") {
      const dto: UpdateModelPropertyDto = {
        projectId,
        key: "81641003928937984",
        value: diagramModel!.id,
        instanceId: tableId as string
      };
      await propertyUtil.updateProperty(dto);
    }

    app.$loading.close();
    app.$notify({ type: "success", message: "想定生成成功", title: "成功" });
  }
}
